<template>
  <div id="add_card">
    <div class="top">
      <van-icon @click="toPurseHome()" name="arrow-left"/>
      <div class="title">添加银行卡</div>
    </div>
    <div class="content">
      <van-form @submit="onSubmit">
        <div class="title">输入卡号绑卡</div>
        <van-field
            v-model="bankCardNumber"
            name="cardNumber"
            placeholder="请输入卡号"
            :rules="[{ required: true, message: '请输入银行卡号' }]"
        />
        <div class="bind">
          <van-button round block type="info" native-type="submit">绑定</van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import {addCard} from "@/api/purse/purse_home";

export default {
  data() {
    return {
      bankCardNumber: "",
      form: {
        bankName: "",
        bankCardNumber: "",
        bankIcon: "",
        userId: "",
      },
    };
  },
  methods: {
    toPurseHome() {
      this.$router.push("/purse_home");
    },
    onSubmit(values) {
      // 从传入的参数 values 中获取 cardNumber 属性的值，并赋值给当前作用域的 bankCardNumber 变量
      this.bankCardNumber = values.cardNumber;
      // 引入 bankcard-validator 模块，用于验证银行卡号格式
      const bankcardValidator = require('bankcard-validator');
      // 使用引入的模块验证当前的银行卡号是否有效，结果存储在 isValid 变量中
      const isValid = bankcardValidator(this.bankCardNumber);
      console.log(isValid);
      if (!isValid) {
        // 如果银行卡号无效，使用 $toast 工具显示“银行卡号格式错误”的提示信息
        this.$toast.fail("银行卡号格式错误");
      } else {
        // 如果银行卡号有效，调用方法执行与银行相关的操作
        this.form.bankCardNumber = values.cardNumber;
        this.form.userId = localStorage.getItem("userId");
        addCard(this.form).then((res) => {
          if (res.data.code === 1) {
            this.$toast.success(res.data.data);
            this.$router.push("/purse_home");
          } else {
            this.$toast.fail(res.data.msg);
          }
        });
      }
    },
  },
  created() {
  },
};
</script>

<style>
body {
  background-color: #F3FCFF;

  #add_card {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;

    .top {
      width: 100%;
      height: 66px;
      background-color: white;
      display: flex;
      align-items: center;
      justify-content: center;
      position: fixed;
      top: 0;

      .van-icon {
        font-size: 26px;
        position: absolute;
        left: 20px;
      }

      .title {
        font-size: 20px;
      }
    }

    .content {
      width: 100%;
      height: 100%;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      margin-top: 66px;

      .title {
        font-size: 20px;
        font-weight: bold;
        margin: 10px 0;
      }

      .van-field {
        width: 360px;
        height: 60px;
        margin: 0 0 10px 0;
        border-radius: 10px;
        border: 1px solid #CDCECE;
        display: flex;
        align-items: center;
        justify-content: center;
      }

      .bind {
        width: 360px;
        height: 60px;
        margin: 30px 0;

        .van-button {
          width: 100%;
          height: 70%;
          font-size: 16px;
          background-color: #4EA9CB;
        }
      }
    }
  }
}
</style>